package com.cqut.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.cqut.dto.LoginDTO;
import com.cqut.dto.LoginInfoDTO;
import com.cqut.dto.UserDTO;
import com.cqut.model.User;
import com.cqut.service.ILoginService;
import com.cqut.service.IUserService;
import com.cqut.util.ResponseUtil;
import com.cqut.util.StringUtil;


/**
 * @author 
 * */

@RestController
@RequestMapping("/api/login")
public class LoginController {
	
	@Resource(name="loginService")
	private ILoginService loginService;
	
	@Resource(name="userService")
	private IUserService userService;

	/**
	 * 学生登录
	 * @param loginDTO
	 * @param errors
	 * @return
	 */
	@RequestMapping(value = "/loginForStudent", method = RequestMethod.POST)
	public ResponseEntity<String> loginForStudent(
		    @Validated
			LoginDTO loginDTO, BindingResult errors) {
		// 如果有错
		if(errors.hasErrors())
			return ResponseUtil.getResponseEntityWhenInvalidReqParams();
		
		Map<String, Object> rs = loginService.loginForStudent(loginDTO);
		
		return getResponseEntity(rs);
	}
	
	/**
	 * 操作员登录
	 * @param loginDTO
	 * @param errors
	 * @return
	 */
	@RequestMapping(value = "/loginForOP", method = RequestMethod.POST)
	public ResponseEntity<String> loginForOM(
			@Validated
			LoginDTO loginDTO, BindingResult errors) {
		// 如果有错
		if(errors.hasErrors())
			return ResponseUtil.getResponseEntityWhenInvalidReqParams();
		
		Map<String, Object> rs = loginService.loginForOP(loginDTO);
		
		return getResponseEntity(rs);
	}
	
	/**
	 * 评委登录
	 * @param loginDTO
	 * @param errors
	 * @return
	 */
	@RequestMapping(value = "/loginForJD", method = RequestMethod.POST)
	public ResponseEntity<String> loginForJD(
			@Validated
			LoginDTO loginDTO, BindingResult errors) {
		// 如果有错
		if(errors.hasErrors())
			return ResponseUtil.getResponseEntityWhenInvalidReqParams();
		
		Map<String, Object> rs = loginService.loginForJD(loginDTO);
		
		return getResponseEntity(rs);
	}
	
	/**
	 * 竞赛管理员登录
	 * @param loginDTO
	 * @param errors
	 * @return
	 */
	@RequestMapping(value = "/loginForCP", method = RequestMethod.POST)
	public ResponseEntity<String> loginForCP(
			@Validated
			LoginDTO loginDTO, BindingResult errors) {
		// 如果有错
		if(errors.hasErrors())
			return ResponseUtil.getResponseEntityWhenInvalidReqParams();
		
		Map<String, Object> rs = loginService.loginForCP(loginDTO);
		
		return getResponseEntity(rs);
	}
	
	

	
	@RequestMapping(value = "/logout", method = RequestMethod.POST)
	public ResponseEntity<String> logout(String token) {
		if(StringUtil.isNullOrEmpty(token))
			return ResponseUtil.getResponseEntityWhenInvalidReqParams();
		
		if(loginService.logout(token))
			return ResponseUtil.getResEntityForPPPWhenSuccess();
		
		return ResponseUtil.getResEntityForPPPWhenFail();
	}
	
	/**
	 * 得到登录信息
	 * @param loginInfo
	 * @return
	 */
	@RequestMapping(value = "/loginInfo", method = RequestMethod.GET)
	public ResponseEntity<String> getLoginInfo(LoginInfoDTO loginInfo) {
		Map<String, Object> rs=userService.findUserById(loginInfo.getCurUserid());
		
		return getResponseEntity(rs);
	}
	
	/**
	 * 获取返回的responseEntity
	 * */
	private ResponseEntity<String> getResponseEntity(Map<String, Object> rs) {
		if(rs == null)
			return ResponseUtil.getResEntityForPPPWhenFail();
		
		JSONObject data = new JSONObject();
		data.put(ResponseUtil.RS_KEY, true);
		data.putAll(rs);
		return ResponseUtil.getResEntityForPPP(data.toJSONString());
	}
	
	/**
	 * 根据当前登录用户的组织结构id查询该用户所属模块
	 * */
	@RequestMapping(value="/sidebar/{userId}", method=RequestMethod.GET)
	public ResponseEntity<String> findModuelsByOrg(
			@PathVariable("userId")
			String userId,LoginInfoDTO loginInfo) {
		
		if (StringUtil.isNullOrEmpty(userId))
			return ResponseUtil.getResponseEntityWhenInvalidReqParams();
		
		List<Map<String, Object>> codeTables = loginService.findModuelsByOrg(userId,loginInfo,0);
		
		return ResponseUtil.getResEntityForGetAndJsonArray(codeTables);
		
	}

	/**
	 * 根据当前登录用户的id查询该用户所属模块
	 * */
	@RequestMapping(value="/moduleAll", method=RequestMethod.GET)
	public ResponseEntity<String> findModuelsAll(
			String userId,LoginInfoDTO loginInfo) {
		
		if (StringUtil.isNullOrEmpty(userId))
			return ResponseUtil.getResponseEntityWhenInvalidReqParams();
		
		List<Map<String, Object>> codeTables = loginService.findModuelsByOrg(userId,loginInfo,1);
		
		return ResponseUtil.getResEntityForGetAndJsonArray(codeTables);
		
	}
	
	

	
}















